
package com.kugou.game.sdk.base;

import com.kugou.framework.base.BaseWorkerFragment;
import com.kugou.framework.v4.Fragment;

import android.os.Bundle;
import android.view.View;

import java.lang.reflect.Field;

/**
 * 描述:SDK耗时操作fragment附加功能类
 *
 * @author jerryliu
 * @since 2015年12月4日 下午2:50:22
 */
public class BaseSDKWorkerFragment extends BaseWorkerFragment {

    private View rootView;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        rootView = view;
    }

    /**
     * you can use this method to find child in rootView,before use this
     * method,execute the method setRootView() first,the method setRootView()
     * will be initialized after onViewCreated().
     * 
     * @param id
     * @return
     */
    protected View findViewById(int id) {
        if (rootView != null) {
            return rootView.findViewById(id);
        } else {
            return null;
        }
    }

    protected void setRootView(View view) {

        this.rootView = view;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        // java.lang.IllegalStateException: Activity has been destroyed
        try {
            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
